// 代码块 importM
import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, // 去掉debugger图标
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

// 父widget
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    )..repeat(); // 让程序和手机的刷新频率统一
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("Title"),
        ),
        floatingActionButton: FloatingActionButton(
            child: const Icon(Icons.stop),
            onPressed: () {
              // _controller.forward();
              _controller.stop();
              // _controller.repeat();
              // _controller.reset();
              // _controller.reverse();
            }),
        body: Center(
            child: Column(
          children: [
            RotationTransition(
              turns: _controller,
              child: const FlutterLogo(
                size: 60,
              ),
            ),
            const SizedBox(
              height: 40,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                    onPressed: () {
                      _controller.forward(); // 正序播放一次
                    },
                    child: const Text("forward")),
                ElevatedButton(
                    onPressed: () {
                      _controller.reverse(); // 倒序播放一次
                    },
                    child: const Text("reverse")),
                ElevatedButton(
                    onPressed: () {
                      _controller.stop(); // 停止播放
                    },
                    child: const Text("stop")),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                    onPressed: () {
                      _controller.reset(); // 重置
                    },
                    child: const Text("reset")),
                ElevatedButton(
                    onPressed: () {
                      _controller.repeat(); // 重复播放
                    },
                    child: const Text("repeat")),
              ],
            )
          ],
        )));
  }
}
